
/************************************************************************************
*   Copyright (c) 2005, Novell Inc.,                                                * 
*   All rights reserved.                                                            *
*                                                                                   *
*   Redistribution and use in source and binary forms, with or without              *
*   modification, are permitted provided that the following conditions              *
*   are met:                                                                        *
*   1.  Redistributions of source code must retain the above copyright              *
*       notice, this list of conditions and the following disclaimer.               *
*   2.  Redistributions in binary form must reproduce the above copyright           *
*       notice, this list of conditions and the following disclaimer in the         *
*       documentation and/or other materials provided with the distribution.        *
*   3.  Neither the name of the Novell nor the names of its contributors            *
*       may be used to endorse or promote products derived from this software       *
*       without specific prior written permission.                                  *
*   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND *
*   ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE           *
*   IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE      *
*   ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE *
*   FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL      *
*   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS         *
*   OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)           *
*   HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT      *
*   LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY       *
*   OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF          *
*   SUCH DAMAGE.                                                                    *
*************************************************************************************/

/*
 * Initial main.c file generated by Glade. Edit as required.
 * Glade will not overwrite this file.
 */

#ifdef HAVE_CONFIG_H
#  include <config.h>
#endif
#include <stdio.h>
#include <gtk/gtk.h>
#include <sys/stat.h>
#include <signal.h>
#include <fcntl.h>
#include <pwd.h>
#include <errno.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
/* vpnlogin headers */
#include "interface.h"
#include "externs.h"
#include "support.h"
#include "profile.h"
#include "CommonUI.h"

Inf_t Inf;
extern char* errString(int, char*);

int getvpnloginLock()
{
    int fd;
    struct flock vpnlogin_flock;
	char filename[MAX_STRING_LEN];
	struct passwd *pw;
	
	if (!(pw = getpwuid(getuid())))
		return 0;
	strcpy(filename, pw->pw_dir);
	strcat(filename, VPNLOGIN_LOCK_FILE);
        
	vpnlogin_flock.l_type=F_WRLCK;
	vpnlogin_flock.l_whence=SEEK_SET;   
	vpnlogin_flock.l_start=0;
	vpnlogin_flock.l_len=0;
		
		fd=open(filename,O_CREAT | O_RDWR, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
	
	if(fd==-1)
	{
		printf("Could not open the file lock file %s.. exiting!!\n", filename);
			printf("*******errno = %d\n", errno);
		exit(1);
	}
	
	if(fcntl(fd, F_SETLK, &vpnlogin_flock) == -1)
	{
		if((errno==EAGAIN) || (errno==EACCES))
		printf("Could not get File Lock for vpnlogin.. Another instance may be already running\n");
		exit(1);
	}
	return 0;
}

void object_set_up()
{
	char userHome[MAX_STRING_LEN] = { '\0' };
	struct passwd *pw;
	if (!(pw = getpwuid(getuid())))
		return ;
	strcpy(userHome, pw->pw_dir);
	strcpy(PLOG_FILE, userHome);
	strcat(PLOG_FILE, PLOG_FILE1);

	Inf.get_connect_client_sock = get_connect_client_sock;
	Inf.get_connect_client_event_poll_sock = get_connect_client_event_poll_sock;
	Inf.print_event = print_evt;
	Inf.connecting_time_update = connecting_time_update;
	Inf.conection_status_update = conection_status_update;
	Inf.on_vpnlogin_destroy_mainWindow = on_vpnlogin_destroy_mainWindow;
	Inf.printing_function = printing_function;
	Inf.updateUptime = updateUptime;
	Inf.refresh = refresh_events;

	Inf.connInProgress = FALSE;
	Inf.runEventPoll = 1;
	Inf.connected = 0;
	Inf.mainWindowActive = FALSE;
	Inf.keepMainWindow = 0;
	Inf.pluginBufLen = 0;
	Inf.withProfileFile = 1;
	Inf.authentication_type = -1;
}

int main (int argc, char *argv[])
{
	GtkWidget *vpnlogin;
	object_set_up();
	setUserEnv(&Inf);
	getvpnloginLock();
	plogset(PLOG_FILE);
	ploginit();
	plog(LLV_INFO, NULL, NULL, "Novell VPN Client for Linux GUI Startup ....\n");

	
	
	#ifdef ENABLE_NLS
	bindtextdomain (GETTEXT_PACKAGE, PACKAGE_LOCALE_DIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
	#endif
	
	gtk_set_locale ();
	gtk_init (&argc, &argv);
	
			/* Commenting this inorder for help not to be visible for Novell innerforge build */
		
	#if 0
	add_pixmap_directory ("/opt/novell" "/" PACKAGE "/pixmaps");
	#endif  
	/*
	* The following code was added by Glade to create one of each component
	* (except popup menus), just so that you see something after building
	* the project. Delete any components that you don't want shown initially.
	*/
	vpnlogin = create_vpnlogin ();
	gtk_widget_show (vpnlogin);
	
	signal(SIGPIPE,SIG_IGN);
	gtk_main ();
	return 0;
}

